package com.xhwl.centiotdata.pollingtrigger.eesd;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.constant.EesdConstant;
import com.xhwl.centiotdata.handler.eesd.EesdFactory;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.pojo.enums.device.DeviceProductCategoryEnum;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.EESDIntegrationConfig;
import com.xhwl.centiotdata.service.integrateconfig.EESDSupplierConfig;
import com.xhwl.centiotdata.utils.ThreadPoolExecutorService;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.enums.eesd.IotProductIdEnum;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 09:25
 */
@Slf4j
@Service
public class EesdDispatcher implements ScheduleDispatcher {

    @Autowired
    private EESDIntegrationConfig integrationConfig;
    @Autowired
    private EESDSupplierConfig supplierConfig;

    @Override
    public void doOneMinutesTask() {
        // iot同步数据
//        Future<?> future = ThreadPoolExecutorService.getThreadPool().submit(() -> {
//            log.info("==================执行iot同步数据正式开始.....");
//            this.executeIotNorth();
//        });
//        try {
//            future.get(60L, TimeUnit.SECONDS);
//        } catch (Exception e) {
//            log.error("==================执行iot同步数据执行异常.....");
//            e.printStackTrace();
//        }
//
//        ThreadPoolUtils.ntRoomExecutor.execute(() -> {
//            log.info("==================执行南投电房环境数据抽取正式开始.....");
//            this.executeTenNtRoomEnvData();
//        });

    }

    @Override
    public void doTenMinutesTask() {
        ThreadPoolUtils.executor.execute(() -> {
            log.info("==================执行水电表设备数据抽取正式开始.....");
            this.pullIotNorthYDZJDevices();
        });
        // 光伏同步数据
        Future<?> future = ThreadPoolExecutorService.getThreadPool().submit(() -> {
            log.info("==================光伏10min同步数据统计开始.....");
            this.executePhotovoltaics();
        });
        try {
            future.get(60L, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("==================光伏10min同步数据统计执行异常.....");
            e.printStackTrace();
        }
        Future<?> future1 = ThreadPoolExecutorService.getThreadPool().submit(() -> {
            log.info("==================iot平台10min数据统计开始.....");
            this.executeIotNorthStatistics();
        });
        try {
            future1.get(60L, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("==================光伏10min同步数据统计执行异常.....");
            e.printStackTrace();
        }
        // 环境同步原始数据
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executeEnvOriginalData();
        });
        // 充电桩同步数据
        ThreadPoolUtils.executor.execute(() -> {
            this.executeCharge();
        });
        //EESD同步天气数据
       ThreadPoolUtils.executor.execute(() -> {
            log.info("==================执行华为天气统计正式开始.....");
            this.executeHwWeather();
        });
        ThreadPoolUtils.alarmExecutor.execute(() -> {
            this.executeBaAlarm();
        });
        ThreadPoolUtils.executor.execute(() -> {
            this.pullRenKe();
        });
        ThreadPoolUtils.executor.execute(() -> {
            log.info("==================执行南投电房其他设备数据抽取正式开始.....");
            this.executeTenNtRoomOtherDeviceData();
        });
    }

    @Override
    public void doTenSecondsTask() {
        //EESD同步南投电房环境
        /*ThreadPoolUtils.executor.execute(() -> {
            log.info("==================执行南投电房环境数据抽取正式开始.....");
             this.executeTenNtRoomEnvData();
        });*/

    }

    @Override
    public void doOneHalfHourTask() {
        //EESD同步南投电房其他设备数据
        /*ThreadPoolUtils.executor.execute(() -> {
            log.info("==================执行南投电房其他设备数据抽取正式开始.....");
            this.executeTenNtRoomOtherDeviceData();
        });*/

    }

    @Override
    public void doFiveSecondsTask() {
    	// 环境同步原始数据
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executeFiveEnvOriginalData();
        });
        // 南投储能车
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executeFiveNtEsCarData();
        });
    }
    
    @Override
    public void doOneHourTask() {
        ThreadPoolUtils.executor.execute(() -> {
            this.executeDevice();
        });
        ThreadPoolUtils.executor.execute(() -> {
            this.executeRecord();
        });

        // sass iot 同步电水表数据
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            log.info("---------同步iot电水表读数数据---------");
            this.executeIotNorth();
        });

        // EESD同步Hour数据
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executeEnvHourData();
        });
        // 环境原始数据推送给告警中台
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executePushEnvData();
        });
        // 望美环境 电量
        ThreadPoolUtils.executor.execute(() -> {
            //延迟1分钟,防止数据还没有更新上来
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("执行望美电量统计正式开始.....");
            this.executeWmzzElectricDevice();
        });
        // 望美环境 水表
        ThreadPoolUtils.executor.execute(() -> {
            //延迟1分钟,防止数据还没有更新上来
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.executeWmzzWaterDevice();
        });
        // 光伏
        Future<?> future = ThreadPoolExecutorService.getThreadPool().submit(() -> {
            log.info("==================光伏hour数据统计开始.....");
            this.executePhotovoltaicsHour();
        });
        try {
            future.get(60L, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("==================光伏hour数据统计执行异常.....");
            e.printStackTrace();
        }
        // 充电桩同步数据（同时零点清零）
        ThreadPoolUtils.executor.execute(() -> {
            this.executeStatisticsCharge();
        });
        //RUNPAQ源牌
        ThreadPoolUtils.executor.execute(() -> {
            this.electricMeterData();
        });
        ThreadPoolUtils.executor.execute(() -> {
            this.executeBESDevice();
        });
        // 科德子系统同步水电表累积量
        ThreadPoolUtils.executor.execute(this::executePullKeDeElectricityUseRecord);
    }

    @Override
    public void doOneDayTask() {
        // 环境同步原始设备数据到设备中台
        ThreadPoolExecutorService.getThreadPool().submit(() -> {
            this.executePushEnvToDeviceData();
        });

    }

    /**
     * 处理华为天气
     */
    public void executeHwWeather() {
        // 获取BA抄表明细处理方案
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.WEATHER.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.WEATHER.getId());
            if (handler == null) {
                log.info("==================8888888888888888888888888执行华为天气统计正式开始.....");
                return;
            }
            log.info("==================222222222222222222222执行华为天气统计正式开始.....");
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void  pullIotNorthYDZJDevices() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                .stream().filter(c -> c.getProductId().equals(EesdMethodEnum.IOT_NORTH.getId())
                        && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.pullIotNorthYDZJDevices(config);
            } catch (Exception e) {
                log.error("同步iot平台数据异常： {}", e.getMessage());
            }
        }
    }

    public void executeRecord() {
        // 获取BA抄表明细处理方案
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.BA_TOWN.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.BA_TOWN.getId());
            if (handler == null) {
                return;
            }
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void executeDevice() {
        // 获取BA设备处理方案
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.BA_TOWN.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.BA_TOWN.getId());
            if (handler == null) {
                return;
            }
            handler.pullDevice(eesdConfigDTO);
        });
    }


    /**
     * 拉取ba告警
     */
    public void executeBaAlarm() {
        log.info("======拉取ba状态和判断告警=============");
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.BA_TOWN.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.BA_TOWN.getId());
            if (handler == null) {
                return;
            }
            handler.pullEnvTenMinusData(eesdConfigDTO);
        });
    }

    /**
     * 仁科环境数据入库
     */
    public void pullRenKe() {
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.RENKE.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.RENKE.getId());
            if (handler == null) {
                return;
            }
            handler.pullEnvTenMinusData(eesdConfigDTO);
        });
    }

    /**
     * 拉取指定项目数据
     *
     * @param projectId
     */
    public void executeRenKeInfo(Integer projectId) {
        // 执行仁科环境监测的逻辑
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.RENKE.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            if (!projectId.equals(eesdConfigDTO.getProjectId())) {
                return;
            }
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.RENKE.getId());
            if (handler == null) {
                return;
            }
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void executeWmzzElectricDevice() {
        // 望美环境电设备以及读数
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.WMZZ.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.WMZZ.getId());
            if (handler == null) {
                log.info("未找到" + EesdMethodEnum.WMZZ.getId() + "的项目数据");
                return;
            }
            eesdConfigDTO.setIsType(1);
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void executeWmzzWaterDevice() {
        // 望美环境水电设备以及读数数据
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.WMZZ.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.WMZZ.getId());
            if (handler == null) {
                return;
            }
            eesdConfigDTO.setIsType(2);
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void executeWmzzEnvNumber() {
        // 望美环境环境数据
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.WMZZ.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.WMZZ.getId());
            if (handler == null) {
                return;
            }
            eesdConfigDTO.setIsType(3);
            handler.pullRecord(eesdConfigDTO);
        });
    }


    public void electricMeterData() {
        // 获取RUNPAQ源牌电表实时数据 Runpaq
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.RUNPAQ.getId());
        log.info("获取RUNPAQ源牌电表实时数据 配置{}", JSONObject.toJSONString(eesdConfigs));
        if (CollectionUtils.isNotEmpty(eesdConfigs)) {
            eesdConfigs.forEach(eesdConfigDTO -> {
                EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.RUNPAQ.getId());
                if (handler == null) {
                    return;
                }
                handler.pullRecord(eesdConfigDTO);
            });
        }
    }


    /**
     * EESD同步原始数据
     */
    public void executeEnvOriginalData() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        for (DeviceProjSupplierCategory config : list) {
            // prodCategoryId-8 系统大类id 能源系统
            // 8 -蓝居； 10 -兴海-iot平台; 11 -e能管家系统
            Integer productId = config.getProductId();
            /**
             * 这个地方设计有点问题. 比如这里希望把某一个项目的这个任务修改执行时间.
             * 这种情况下是不支持的. 所以这里写死projectId
             */
            if(Integer.valueOf(2).equals(config.getProjectId()) && Integer.valueOf(10).equals(productId)) {
        		continue;
        	}
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
                continue;
            }
            try {
                handler.pullEnvData(config);
            } catch (Exception e) {
                log.error("EESD同步原始数据异常：projectId={},msg={}", config.getProjectId(), e.getMessage());
            }
        }
    }

    
    /**
     * EESD同步原始数据v2 5秒钟执行一次
     */
    public void executeFiveEnvOriginalData() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        for (DeviceProjSupplierCategory config : list) {
            // prodCategoryId-8 系统大类id 能源系统
            // 8 -蓝居； 10 -兴海-iot平台; 11 -e能管家系统
        	Integer productId = config.getProductId();
        	if(Integer.valueOf(2).equals(config.getProjectId()) && Integer.valueOf(10).equals(productId)) {
        		EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
        		if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
        			continue;
        		}
        		try {
        			handler.pullEnvData(config);
        		} catch (Exception e) {
        			log.error("EESD同步原始数据异常：projectId={},msg={}", config.getProjectId(), e.getMessage());
        		}
        	}
        }
    }
    /**
     * 环境同步原始数据到设备中台
     */
    public void executePushEnvToDeviceData() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
                continue;
            }
            try {
                handler.pushEnvToDeviceData(config);
            } catch (Exception e) {
                log.error("环境同步原始数据到设备中台异常： {}", e.getMessage());
            }
        }
    }

    /**
     * EESD同步Hour数据
     */
    public void executeEnvHourData() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
                continue;
            }
            try {
                handler.pullEnvHourData(config);
            } catch (Exception e) {
                log.error("EESD同步Hour数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 环境原始数据推送给告警中台
     */
    public void executePushEnvData() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
                continue;
            }
            try {
                handler.pushEnvData(config);
            } catch (Exception e) {
                log.error("环境原始数据推送给告警中台异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步光伏数据
     */
    public void executePhotovoltaics() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        list = list.stream().filter(c -> c.getProductId().equals(EesdMethodEnum.GROWATT.getId())
                && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.pullPhotovoltaics(config);
            } catch (Exception e) {
                log.error("光伏同步原始数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步光伏统计数据
     */
    public void executePhotovoltaicsHour() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        list = list.stream().filter(c -> c.getProductId().equals(EesdMethodEnum.GROWATT.getId())
                && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.pullPhotovoltaicsHour(config);
            } catch (Exception e) {
                log.error("光伏同步统计数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步充电桩数据
     */
    public void executeCharge() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        list = list.stream().filter(c -> c.getProductId().equals(EesdMethodEnum.QI_MING.getId())
                && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler) || config.getProperties().isEmpty()) {
                continue;
            }
            try {
                handler.pullCharge(config);
            } catch (Exception e) {
                log.error("同步充电桩数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 统计充电桩数据（时，日，月）
     */
    private void executeStatisticsCharge() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId());
        list = list.stream().filter(c -> c.getProductId().equals(EesdMethodEnum.QI_MING.getId())
                && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.statisticsCharge(config);
            } catch (Exception e) {
                log.error("统计充电桩数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步南投电房环境
     */
    private void executeTenNtRoomEnvData(){
        // 拉取南投储能车采集数据
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.NT_ELROOM.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.NT_ELROOM.getId());
            if (handler == null) {
                log.info("未获取到对应南投电房处理器");
                return;
            }
            log.info("拉取南投电房采集数据开始.....");
            handler.pullEnvData(eesdConfigDTO);
        });
    }

    /**
     * 同步南投电房其他设备
     */
    private void executeTenNtRoomOtherDeviceData(){
        // 拉取南投储能车采集数据
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.NT_ELROOM.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.NT_ELROOM.getId());
            if (handler == null) {
                log.info("未获取到对应南投电房处理器");
                return;
            }
            log.info("拉取南投电房采集数据开始.....");
            handler.pullRecord(eesdConfigDTO);
        });
    }

    /**
     * 同步南投储能车
     */
    private void executeFiveNtEsCarData(){
        // 拉取南投储能车采集数据
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.NT_ESCAR.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.NT_ESCAR.getId());
            if (handler == null) {
                log.info("未获取到对应储能车处理器");
                return;
            }
            log.info("拉取南投储能车采集数据开始.....");
            handler.pullRecord(eesdConfigDTO);
        });
    }

    public void executeBESDevice() {
        // 获取BES设备处理方案
        List<EesdConfigDTO> eesdConfigs = integrationConfig.getIntegrationConfig(EesdMethodEnum.BES.getId());
        eesdConfigs.forEach(eesdConfigDTO -> {
            EesdHandler handler = EesdFactory.getInvokeStrategy(EesdMethodEnum.BES.getId());
            if (handler == null) {
                return;
            }
            handler.pullDevice(eesdConfigDTO);
        });
    }

    /**
     * 南飞鸿拉取科德水电子系统水电量累计使用量记录
     */
    public void executePullKeDeElectricityUseRecord() {
        List<EesdConfigDTO> configDTOList = integrationConfig.getIntegrationConfig(EesdMethodEnum.KEDE.getId());
        configDTOList.forEach(configDTO -> {
            EesdHandler invokeStrategy = EesdFactory.getInvokeStrategy(EesdMethodEnum.KEDE.getId());
            if (Objects.isNull(invokeStrategy)) {
                log.info("未找到KeDeHandler");
                return;
            }
            invokeStrategy.pullRecord(configDTO);
        });
    }


    /**
     * 同步iot平台数据
     */
    public void executeIotNorth() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                        .stream().filter(c -> c.getProductId().equals(EesdMethodEnum.IOT_NORTH.getId())
                        && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            log.info("handler:{}",handler);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.pullIotNorthData(config);
                // 数据源为南向iot设备时 无需修改设备状态 对接第三方设备数据时 需要
//                handler.pullIotNorthDeviceStatus(config);
            } catch (Exception e) {
                log.error("同步iot平台数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步iot平台设备数据
     */
    public void executeIotNorthDevices() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                .stream().filter(c -> c.getProductId().equals(EesdMethodEnum.IOT_NORTH.getId())
                        && !c.getProperties().isEmpty()).collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.pullIotNorthDevices(config);
            } catch (Exception e) {
                log.error("iot同步原始数据异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 暂时iot平台设备冰蓄冷数据统计
     */
    public void executeIotNorthStatistics() {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                .stream()
                .filter(c -> c.getProductId().equals(EesdMethodEnum.IOT_NORTH.getId()) && !c.getProperties().isEmpty())
                .filter(c -> IotProductIdEnum.ICE_STORAGE_AIR.equals( IotProductIdEnum.getByType(c.getProperties().getString(EesdConstant.EESD_DEVICE_TYPE_CODE))))
                .collect(Collectors.toList());
        for (DeviceProjSupplierCategory config : list) {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                continue;
            }
            try {
                handler.iotNorthStatistics(config);
            } catch (Exception e) {
                log.error("iot平台设备数据统计异常： {}", e.getMessage());
            }
        }
    }

    /**
     * 同步科德水费记录
     */
    public void syncKeDeWaterRecord(Integer intervalDays, String deviceNo) {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                .stream()
                .filter(c -> c.getProductId().equals(EesdMethodEnum.KEDE.getId()) && !c.getProperties().isEmpty())
                .collect(Collectors.toList());
        list.forEach(config -> {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                return;
            }
            try {
                handler.pullKeDeWaterPaymentData(config, intervalDays, deviceNo);
            } catch (Exception e) {
                log.error("同步科德水费充值数据异常： {}", e.getMessage());
            }
        });
    }

    /**
     * 同步科德电费记录
     */
    public void syncKeDeElectricityRecord(Integer intervalDays, String deviceNo) {
        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
                .stream()
                .filter(c -> c.getProductId().equals(EesdMethodEnum.KEDE.getId()) && !c.getProperties().isEmpty())
                .collect(Collectors.toList());
        list.forEach(config -> {
            Integer productId = config.getProductId();
            EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
            if (Objects.isNull(handler)) {
                return;
            }
            try {
                handler.pullKeDeElectricityPaymentData(config, intervalDays, deviceNo);
            } catch (Exception e) {
                log.error("同步科德电费充值数据异常： {}", e.getMessage());
            }
        });
    }

}


